package org.zero.onlinesubject.controller;


import org.springframework.web.bind.annotation.*;
import org.zero.onlinesubject.common.vo.HomeworkVo;
import org.zero.onlinesubject.common.vo.Result;
import org.zero.onlinesubject.entity.Homework;
import org.zero.onlinesubject.service.HomeworkService;
import org.zero.onlinesubject.utils.UserContextHolder;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/homework/api")
public class HomeworkController {

    @Resource
    private HomeworkService homeworkService;

    @PostMapping("/addHomework")
    public Result<String> createHomework(@RequestBody HomeworkVo homeworkVo) throws ExecutionException, InterruptedException, TimeoutException {
        Boolean flag = homeworkService.createHomework(homeworkVo);
        return Result.ok();
    }

    @GetMapping("/homeworkList")
    public Result<List<Homework>> getHomeworkList(Integer current, Integer size){
        if(size == 0){
            size = 10;
        }
        if(current != 0) current = (current - 1) * size;
        String userId = UserContextHolder.get().getUserid();
        List<Homework> list = homeworkService.getHomeworkList(userId,current,size);

        return Result.ok(list);
    }

    @GetMapping("/getPubList")
    public Result<List<Homework>> getPubList(Integer current,Integer size){
        if(size == 0){
            size = 10;
        }
        if(current != 0) current = (current - 1) * size;
        String userId = UserContextHolder.get().getUserid();
        List<Homework> list = homeworkService.getPubList(userId,current,size);
        return Result.ok(list);
    }

    @GetMapping("getApprovalList")
    public Result<List<HomeworkVo>> getApprovalList(String classId,String courseId,String approval,Integer current,Integer size){
        if(size == 0){
            size = 10;
        }
        if(current != 0) current = (current - 1) * size;
        List<HomeworkVo> voList = homeworkService.getApprovalList(classId,courseId,approval,current,size);
        return Result.ok(voList);
    }

    @GetMapping("/getHomeworkInfo")
    public Result<HomeworkVo> getHomework(Integer homeworkId) throws ExecutionException, InterruptedException, TimeoutException {
        return Result.ok(homeworkService.getHomeworkVo(homeworkId));
    }

    // 提交作业 或 批改作业
    @PostMapping("/updateHomework")
    public Result<String> updateHomework(@RequestBody HomeworkVo homeworkVo){
        if(homeworkVo==null) return Result.error(500,"作业信息不存在");
        Boolean flag = homeworkService.updateHomework(homeworkVo);
        return flag ? Result.ok("更新作业信息成功") : Result.error(500,"更新作业信息失败");
    }

    // 删除作业信息
    @GetMapping("/delHomework")
    public Result<String> deleteHomework(Integer homeworkId){
        if(homeworkId==0) return Result.error(500,"课程ID不能为空");
        Boolean flag = homeworkService.deleteHomework(homeworkId);
        return flag ? Result.ok("删除课程成功") : Result.error(500,"删除课程失败");
    }

    // 计算作业分数
    @PostMapping("/calcScore")
    public Result<HomeworkVo> calcScore(@RequestBody HomeworkVo homeworkVo) {
        Double score = homeworkService.calcScore(homeworkVo);
        homeworkVo.setTotalScore(score);
        return score != null ? Result.ok(homeworkVo) : Result.error(500,"计算错误");
    }
}